/*
 * Copyright (C) 2021 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.dona.imsidetector.adpter;

import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dona.imsidetector.R;
import com.dona.imsidetector.db.entity.ListItem;
import com.dona.imsidetector.fragment.BlackWhiteFragment;
import com.dona.imsidetector.utils.IMSIUtil;

import java.util.List;

public class BlackWhiltListItemAdapter extends RecyclerView.Adapter<BlackWhiltListItemAdapter.ViewHolder> {

    private static final String TAG = "BWListItemAdapter";
    private List<ListItem> listItems;

    private LayoutInflater layoutInflater;

    private BlackWhiteFragment context;


    private View prevClickView;
    private boolean multChoice = false;


    public BlackWhiltListItemAdapter(BlackWhiteFragment context, List<ListItem> listItems) {
        this.context = context;
        this.listItems = listItems;
        layoutInflater = LayoutInflater.from(context.getActivity());
    }

    public List<ListItem> getListItems() {
        return listItems;
    }

    public void setListItems(List<ListItem> listItems) {
        this.listItems = listItems;
    }


    public void setViewSelected(boolean selected, View view) {
        if (selected) {
            view.setBackgroundColor(Color.WHITE);
            view.setBackground(context.getResources().getDrawable(R.drawable.list_radius_background_select));
        } else {
            view.setBackground(context.getResources().getDrawable(R.drawable.list_radius_background_normal));
        }

    }


//    public void itemSelect(ViewHolder holder,View view){
//        view.setBackgroundColor(Color.WHITE);
//        view.setBackground(context.getResources().getDrawable(R.drawable.list_radius_background_select));
//        if(null != prevClickView && !view.equals(prevClickView)){
//            prevClickView.setBackground(context.getResources().getDrawable(R.drawable.list_radius_background_normal));
//        }
//        prevClickView = view;
//        if(context.operationsImsiItemList.size() == 0){
//            context.operationsImsiItemList.add(0, holder.imsiItem);
//        }else {
//            context.operationsImsiItemList.clear();
//            context.operationsImsiItemList.add(0, holder.imsiItem);
//        }
//        Log.e(TAG, "position: " + holder.index + "  itemSelect: " + holder.imsiItem.toString());
//
//    }

    public void itemSelect(BlackWhiltListItemAdapter.ViewHolder holder) {
        if (multChoice) {

        } else {
            if (null != prevClickView) {
                if (prevClickView.equals(holder.imsiItemView)) {
                    setViewSelected(false, holder.imsiItemView);
                    context.deleteOperationImsiItemList(holder.listItem);
                    prevClickView = null;
                } else {
                    setViewSelected(true, holder.imsiItemView);
                    setViewSelected(false, prevClickView);
                    if (context.operationsListItemList.size() == 0) {
                        context.addOperationImsiItemList(holder.listItem);
                    } else {
                        context.operationsListItemList.clear();
                        context.addOperationImsiItemList(holder.listItem);
                    }
                    prevClickView = holder.imsiItemView;
                }
            } else {
                setViewSelected(true, holder.imsiItemView);
                prevClickView = holder.imsiItemView;
                if (context.operationsListItemList.size() == 0) {
                    context.addOperationImsiItemList(holder.listItem);
                } else {
                    context.operationsListItemList.clear();
                    context.addOperationImsiItemList(holder.listItem);
                }
            }

            Log.e(TAG, "position: " + holder.index + "  itemSelect: " + holder.listItem.getImsiAddr());
            Log.e(TAG, "operationsImsiItemList: " + context.operationsListItemList.size());
        }


    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.adapter_black_white_imsi_list_item_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
//        Log.e(TAG, "onCreateViewHolder: " + holder.toString() );
        holder.imsiItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.imsiItemView = v;
                itemSelect(holder);

            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ListItem listItem = listItems.get(position);
        holder.listItem = listItem;
        Log.e("onBindViewHolder", "position: " + position);

        holder.index = position;
        holder.tvIsWhite.setText(holder.listItem.getIsWhite() == 1 ? "白" : "黑");
        holder.tvImsiAddress.setText(listItem.getImsiAddr());
        holder.tvIsp.setText(IMSIUtil.tranferImsiType(listItem.getIsp()));
        holder.tvProvince.setText(listItem.getProvince());
        holder.tvCity.setText(listItem.getCity());
    }

    @Override
    public int getItemCount() {
        return listItems.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private static final String TAG = "ViewHolder";

        ListItem listItem;

        View imsiItemView;
        TextView tvImsiAddress;
        TextView tvIsWhite;
        TextView tvIsp;
        TextView tvProvince;
        TextView tvCity;
        public int index;

        public ViewHolder(View view) {
            super(view);
            imsiItemView = view;

            tvImsiAddress = (TextView) view.findViewById(R.id.imsiAddr);
            tvIsWhite = (TextView) view.findViewById(R.id.isWhite);
            tvIsp = (TextView) view.findViewById(R.id.isp);
            tvProvince = (TextView) view.findViewById(R.id.province);
            tvCity = (TextView) view.findViewById(R.id.city);
        }


    }
}
